//
//  SystemSettingsViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/15.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "SystemSettingsViewController.h"
#import "RDVTabBarController.h"
#import "AboutUsViewController.h"
#import "PrinterSDK.h"
#import "SVProgressHUD.h"
#import <PgyUpdate/PgyUpdateManager.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "TheLoginViewController.h"
#import "PrinterListViewController.h"
#import <RongIMKit/RongIMKit.h>
#import "AboutUsViewController.h"
@interface SystemSettingsViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate, UITextViewDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,RCIMConnectionStatusDelegate,RCIMReceiveMessageDelegate>
{
    
    NSArray *array;
    NSArray *array1;
    NSMutableArray*deviceList;
    UILabel*Printer;
    UILabel*CFBundleVersion;
}

@end

@implementation SystemSettingsViewController
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.view.backgroundColor=[UIColor whiteColor];
    
    [self scanPrinter:nil ];
    
}
- (void)scanPrinter:(id)sender {

    
    [PrinterWraper SetBlutoothDelegate:self];
    [PrinterWraper StartScanTimeout:10];
 
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [PrinterWraper StopScan];
    [PrinterWraper SetBlutoothDelegate:nil];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self tableHeadView];
    
    self.title = @"系统设置";
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
}


//表头视图
- (void)tableHeadView
{
    
    
    array = @[@"连接打印机"];
    array1= @[@"关于我们",@"当前版本"];
    _table.frame=CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token==nil) {
        
    }else{
        
        UIView * tableFooterViews=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)];
        tableFooterViews.backgroundColor=zuiqianhuiColor;
        UIView*huiseview=[[UIView alloc]initWithFrame:CGRectMake(0,60, ScreenWidth, 50)];
        huiseview.backgroundColor=[UIColor whiteColor];
        huiseview.layer.borderWidth = 0.3;
        huiseview.layer.borderColor = qianhuiColor.CGColor;
        [tableFooterViews addSubview:huiseview];
        UIButton*exit=[[UIButton alloc]init];
        exit.backgroundColor=[UIColor clearColor];
        [exit setTitle:@"退出登录" forState:UIControlStateNormal];
        exit. titleLabel.font=[UIFont systemFontOfSize:16];
        [exit setTitleColor:shenhongseColor forState:UIControlStateNormal];
        exit.frame=CGRectMake(0, 0, ScreenWidth, 50);
        [exit addTarget:self action:@selector(exit:) forControlEvents:UIControlEventTouchUpInside];
        [huiseview addSubview:exit];
        _table.tableFooterView = tableFooterViews;
        
    }
    
    _table.backgroundColor=zuiqianhuiColor;

}
-(void)exit:(UIButton*)sender{
    
    UIAlertView* alts = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"是否确认注销？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alts show];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag==1) {
        if (buttonIndex == 0) {
            
//            NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app"];
//            [[UIApplication sharedApplication] openURL:url];
//            
        }
        
    }else{

    if (buttonIndex == 1) {
        
        NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
        NSString* tokenId = [user1 objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:tokenId forKey:@"tokenId"];
        [HttpRequest requestData1:@"Common/LoginOut" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    NSInteger status0=  [[RCIMClient sharedRCIMClient] getConnectionStatus];
                    NSLog(@"myviewcontrollerstatus00^^^===%ld",(long)status0);
                    
                    [[RCIMClient sharedRCIMClient] disconnect:NO];
                    
                    [Global setRongYunToken:@""];
                    [Global hideProgressHUDInternal:self.view];
                    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                    [user setObject:@"out" forKey:@"tokenId"];
                    [user synchronize];
                    TheLoginViewController* Login=[[TheLoginViewController alloc]init];
                    //                    Login.mDelegate=self;
                    [self.navigationController pushViewController:Login animated:YES];
                    NSInteger status=  [[RCIMClient sharedRCIMClient] getConnectionStatus];
                    NSLog(@"myviewcontrollerstatus^^^===%ld",(long)status);
                    
                }else{
                    
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        TheLoginViewController*detailViewController = [[TheLoginViewController alloc] init];
                        //                        detailViewController.mDelegate=self;
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    }
                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:severDataDict.resultStr view:self.view];
            }
            
        }];
    }
    }

}




#pragma mark--TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2 ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return array.count;
    }if (section == 1) {
        return array1.count;
    }
    return array.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
       cell.textLabel.textColor=qianheiColor;
    cell.textLabel.font=[UIFont systemFontOfSize:16];
   if (indexPath.section == 0) {
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    
    [Printer removeFromSuperview];
    Printer=[[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-210, 0, 170, 50)];
    Printer.font=[UIFont systemFontOfSize:14];
    Printer.textColor=zhonghuiColor;
    Printer .textAlignment=NSTextAlignmentRight;
    [cell.contentView addSubview:Printer];
    if (deviceList.count>0) {
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Device = [user objectForKey:@"DeviceName"];

        CBPeripheral *device=[deviceList objectAtIndex:0];
        NSString*name= device.name.length>0?device.name:NSLocalizedString(@"目前没有设备与打印机连接", @"");
        if ([Device isEqualToString:name]) {
          
            Printer.text=name;
        
        }else{
        
        Printer.text=@"目前没有设备与打印机连接";
        }
       
    }else{
        
        
        Printer.text=@"目前没有设备与打印机连接";
    }
    
    if (array.count ==indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 50-0.40, ScreenWidth, 0.40)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
        
    }
       UIButton*jiantou = [[UIButton alloc]init];
       jiantou.frame = CGRectMake(ScreenWidth-40,10, 30, 30);
       jiantou.userInteractionEnabled=NO;
       [jiantou setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
       [cell.contentView addSubview:jiantou];

   }else{
    
        cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
       if (indexPath.row==0) {
           
           UILabel *lineLabel11=[[UILabel alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, 0.40)];
           lineLabel11.backgroundColor=qianhuiColor;
           [cell.contentView addSubview:lineLabel11];
           UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 50-0.40, ScreenWidth-20, 0.3)];
           lineLabel1.backgroundColor=qianhuiColor;
           [cell.contentView addSubview:lineLabel1];
           UIButton*jiantou = [[UIButton alloc]init];
           jiantou.frame = CGRectMake(ScreenWidth-40,10, 30, 30);
           jiantou.userInteractionEnabled=NO;
           [jiantou setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
           [cell.contentView addSubview:jiantou];

       }else{
           UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 50-0.40, ScreenWidth, 0.40)];
           lineLabel1.backgroundColor=qianhuiColor;
           [cell.contentView addSubview:lineLabel1];
           [CFBundleVersion removeFromSuperview];
           CFBundleVersion=[[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-210, 0, 170, 50)];
           CFBundleVersion.font=[UIFont systemFontOfSize:14];
           CFBundleVersion.textColor=zhonghuiColor;
           CFBundleVersion .textAlignment=NSTextAlignmentRight;
           [cell.contentView addSubview:CFBundleVersion];
           
           NSString *getVersion =  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
           CFBundleVersion.text =getVersion;

       }
   }

    return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell =(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if ([cell.textLabel.text isEqualToString:@"连接打印机"]) {
        
        PrinterListViewController * Login=[[PrinterListViewController alloc]init];
        Login.shi=@"shida";
        [self.navigationController pushViewController:Login animated:YES];
        
    }else
    if ([cell.textLabel.text isEqualToString:@"关于我们"]) {
        
        AboutUsViewController * Login=[[AboutUsViewController alloc]init];
        [self.navigationController pushViewController:Login animated:YES];
        
    }else
        if ([cell.textLabel.text isEqualToString:@"当前版本"]) {
            

            
        }

    
}

#pragma mark bluetooth delegate
-(void)BlueToothOpen:(BOOL)isopen;{

    
    deviceList=[[NSMutableArray alloc]init];
    deviceList=nil;
    [self.table reloadData];
}

-(void)updateBluetoothDevice:(NSMutableArray*)devices;
{
    
    deviceList=[[NSMutableArray alloc]init];
    deviceList  =devices;
    [self.table reloadData];
}


-(void)requestData:(UIButton *)sender{
    
    TheLoginViewController* Login=[[TheLoginViewController alloc]init];
    
    [self.navigationController pushViewController:Login animated:YES];
    
    
    
}


@end
